﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PMC.Structs;

namespace VacationExtension
{
    public partial class VacationWebsitesAddForm : Form
    {
        private Website _selectedWebsite;
        public Website SelectedWebsite
        {
            get { return _selectedWebsite; }
        }

        private IBindingList excludedWebsites;
        int _Column = 0;
        int _Row = 0;

        public VacationWebsitesAddForm(IBindingList list)
        {
            InitializeComponent();
            excludedWebsites = list;
            cbWebsites.ColumnNames = "Name;Script";
            cbWebsites.ColumnWidths = (cbWebsites.Width / 2) + ";" + (cbWebsites.Width / 2);
            cbWebsites.DataSource = excludedWebsites;
            cbWebsites.DisplayMember = "Name";
            Init("Name;Script");
        }

        private void Init(string columns)
        {
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.MultiSelect = false;
            var column = columns.Split(new[] { ";", "," }, StringSplitOptions.RemoveEmptyEntries);

            dataGridView1.AutoGenerateColumns = false;
            //Set the datasource of the grid = datasource of the combobox
            dataGridView1.DataSource = cbWebsites.DataSource;

            foreach (string str in column)
            {
                var c = new DataGridViewTextBoxColumn();
                c.Name = str;
                c.DataPropertyName = str;
                dataGridView1.Columns.Add(c);
            }
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            _selectedWebsite = (Website)cbWebsites.SelectedItem;
            excludedWebsites.Remove((Website)cbWebsites.SelectedItem);
            Close();
        }

        private void SearchGrid()
        {
            //maxSearches = the # of cells in the grid
            int maxSearches = dataGridView1.Rows.Count * dataGridView1.Columns.Count + 1;
            int idx = 1;
            bool isFound = false;
            string searchValue = txtSearch.Text.ToUpper();

            if (Convert.ToBoolean(txtSearch.Text.Length))
            {
                // If the item is not found and you haven't looked at every cell, keep searching
                while ((!isFound) & (idx < maxSearches))
                {
                    // Only search visible cells
                    if (dataGridView1.Columns[_Column].Visible)
                    {
                        // Do all comparing in UpperCase so it is case insensitive
                        if (dataGridView1[_Column, _Row].Value.ToString().ToUpper().Contains(searchValue))
                        {
                            // If found position on the item
                            dataGridView1.FirstDisplayedScrollingRowIndex = _Row;
                            dataGridView1[_Column, _Row].Selected = true;
                            isFound = true;
                        }
                    }

                    // Increment the column.
                    _Column++;

                    // If it exceeds the column count
                    if (_Column == dataGridView1.Columns.Count)
                    {
                        _Column = 0; //Go to 0 column
                        _Row++;      //Go to the next row

                        // If it exceeds the row count
                        if (_Row == dataGridView1.Rows.Count)
                        {
                            _Row = 0; //Start over at the top
                        }
                    }

                    idx++;
                }
            }
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SearchGrid();
            }
            dataGridView1_DoubleClick(null, EventArgs.Empty);
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            cbWebsites.SelectedIndex = dataGridView1.CurrentRow.Index;
        }
    }
}
